using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SQLDAL;
using System.Data.SqlClient;
using System.Text;
using System.Web.Services;

public partial class GUI_Webpage_OrderFood : System.Web.UI.Page
{

    #region Fields
    FoodBll foodBll = new FoodBll();
    TypeOfFoodInfo tofInfo = new TypeOfFoodInfo();
    RestaurantBll resBll = new RestaurantBll();
    TypeOfFoodBll tofBll = new TypeOfFoodBll();
    FoodInfo foodInfo = new FoodInfo();
    OrderFoodBll orderFoodBll = new OrderFoodBll();
    OrderingFoodDetailsInfo ofdInfo = new OrderingFoodDetailsInfo();

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["bookingID"] == null)
        {
            Random random = new Random();
            StringBuilder strBuilder = new StringBuilder(5);
            for (int i = 0; i < 5; i++)
                strBuilder.Append(random.Next(0, 9).ToString());
            Session["bookingID"] = strBuilder.ToString();
        }
        if (!IsPostBack)
        {
            resBll.LoadRestaurant(drpRestaurant);
            tofBll.LoadTypeOfFood(drpTypeFood);
            Prev.Visible = false;
            Next.Visible = false;
            First.Visible = false;
            Last.Visible = false;
            
            
        }
    }

    private void ListFoodByTypeToDataList(TypeOfFoodInfo tofInfo)
    {
        //the current page
        int startRecord = (int.Parse(CurrentPage.Value) - 1) *
           int.Parse(PageSize.Value);

        SqlParameter[] parms = {
            new SqlParameter("@typeID", SqlDbType.Int, 4)
        };
        parms[0].Value = tofInfo.TypeID;
        DataSet ds = SQLHelper.GetDataSetUsePaging(CommandType.StoredProcedure,
            "sp_Food_ListFoodByType", "Food", startRecord, int.Parse(PageSize.Value),parms);

        //DataBind the DataList    
        dlListFoodByType.DataSource = ds.Tables["Food"];
        dlListFoodByType.DataBind();

        //Get total record
        TotalSize.Value = GetTotalSize().ToString();
        BuildPagers();

    }

    protected void btnFind_Click(object sender, EventArgs e)
    {


        CurrentPage.Value = 1.ToString();
        if (IsPostBack)
        {
            tofInfo.TypeID = int.Parse(drpTypeFood.SelectedValue.ToString());
            ListFoodByTypeToDataList(tofInfo);
            //for(int i = 0; i<5; i++)
                //strBuilder.Append(random.Next(0,9).ToString());
            //txtRandom.Text = Session["bookingID"].ToString();
        }
        
    }

    protected void dlListFoodByType_ItemCommand(object source, DataListCommandEventArgs e)
    {
        DataListItem dlItem = e.Item;
        Label foodID = (Label)dlItem.FindControl("lblFoodID");
        Label foodName = (Label)dlItem.FindControl("lblFoodName");
        Label numberOfPerson = (Label)dlItem.FindControl("lblNumberPerson");
        Label price = (Label)dlItem.FindControl("lblPrice");
        //TextBox quantity = (TextBox)dlItem.FindControl("txtQuantity");

        foodInfo.FoodID = foodID.Text;
        foodInfo.FoodName = foodName.Text;
        foodInfo.NumberOfPerson = int.Parse(numberOfPerson.Text);
        foodInfo.Price = float.Parse(price.Text);

        AddToOrderFoodDetails(foodInfo);
    }

    protected void btnAddToOrder_Click(object sender, EventArgs e)
    {
       // Button b = (Button)sender;
       // ofdInfo.FoodID = b.CommandArgument;
       // AddToOrderFoodDetails(foodInfo);

    }

    protected void drpTime_SelectedIndexChanged(object sender, EventArgs e)
    {
     
    }
    protected void btnViewCart_Click(object sender, EventArgs e)
    {
        String resSelect = drpRestaurant.SelectedValue.ToString();
        String timeSelect = drpTime.SelectedValue.ToString().Trim();
        Response.Redirect("CompleteOrderFood.aspx?resID="+resSelect+"&timeSelect="+timeSelect.Trim()+"");
    }



    #region Get total record
    public Object GetTotalSize()
    {
        SqlParameter[] parms = {
            new SqlParameter("@typeID", SqlDbType.Int, 4)
        };
        parms[0].Value = tofInfo.TypeID;
        return SQLHelper.ExecuteScalar(SQLHelper.CONN_STRING, CommandType.StoredProcedure,
                   "sp_Food_CountFoodOfType", parms);
    }
    #endregion

    #region Method using to navigate the page
    public void Page_DataList(object sender, EventArgs e)
    {
        //Check for Button clicked
        if (((LinkButton)sender).ID == "Prev")
        {
            //Check if we are on any page greater than 0 
            if ((int.Parse(CurrentPage.Value) - 1) >= 0)
            {
                //Decrease the CurrentPage Value
                CurrentPage.Value = (int.Parse(CurrentPage.Value) - 1).ToString();
            }
        }
        else if (((LinkButton)sender).ID == "Next")
        {
            //Check if we can display the next page.
            if ((int.Parse(CurrentPage.Value) * int.Parse(PageSize.Value)) < int.Parse(TotalSize.Value))
            {
                //Increment the CurrentPage value
                CurrentPage.Value = (int.Parse(CurrentPage.Value) + 1).ToString();
            }
        }
        else if (((LinkButton)sender).ID == "First")
        {
            // navigate page to the first page
            CurrentPage.Value = 1.ToString();
        }
        else if (((LinkButton)sender).ID == "Last")
        {
            //navigate page to the last page
            int a = int.Parse(TotalSize.Value);
            int b = int.Parse(PageSize.Value);
            int result = a / b + 1;
            CurrentPage.Value = result.ToString();
        }
        //Reload datalist
        tofInfo.TypeID = Int32.Parse(drpTypeFood.SelectedValue);
        ListFoodByTypeToDataList(tofInfo);
    }
    #endregion

    #region To enable prev/next/first/last button
    private void BuildPagers()
    {
        Prev.Visible = true;
        Next.Visible = true;
        First.Visible = true;
        Last.Visible = true;

        if (int.Parse(CurrentPage.Value) == 1)
        {
            if (int.Parse(TotalSize.Value) <= int.Parse(PageSize.Value))
            {
                First.Enabled = false;
                Prev.Enabled = false;
                Next.Enabled = false;
                Last.Enabled = false;
            }
            else
            {
                First.Enabled = false;
                Prev.Enabled = false;
                Next.Enabled = true;
                Last.Enabled = true;
            }
        }
        else if (int.Parse(CurrentPage.Value) == (int.Parse(TotalSize.Value) / int.Parse(PageSize.Value) + 1))
        {
            First.Enabled = true;
            Prev.Enabled = true;
            Next.Enabled = false;
            Last.Enabled = false;
        }
        else
        {
            First.Enabled = true;
            Prev.Enabled = true;
            Next.Enabled = true;
            Last.Enabled = true;
        }
    }
    #endregion

    #region Method add a food to order cart
    private void AddToOrderFoodDetails(FoodInfo foodInfo)
    {
        DataTable dataTable = new DataTable();
        if (Session["OrderFoodCart"] == null)
        {
            dataTable = new DataTable();
            dataTable.Columns.Add("FoodID");
            dataTable.Columns.Add("FoodName");
            dataTable.Columns.Add("NumberOfPerson");
            dataTable.Columns.Add("Price");
            dataTable.Columns.Add("Quantity");
        }
        else
        {
            dataTable = (DataTable)Session["OrderFoodCart"];
        }
        int indexOfItem = IsExistFoodInOrderCart(foodInfo.FoodID, dataTable);
        //check if food is exist or not
        if (indexOfItem != -1)
        {
            dataTable.Rows[indexOfItem]["Quantity"] = Convert.ToInt32(dataTable.Rows[indexOfItem]["Quantity"]) + 1;
        }
        else
        {
            //if is not exist any food, add a food in Order Cart
            DataRow dataRow = dataTable.NewRow();
            dataRow["FoodID"] = foodInfo.FoodID;
            dataRow["FoodName"] = foodInfo.FoodName;
            dataRow["NumberOfPerson"] = foodInfo.NumberOfPerson.ToString();
            dataRow["Price"] = foodInfo.Price.ToString();
            dataRow["Quantity"] = "1";
            dataTable.Rows.Add(dataRow);
        }
        Session["OrderFoodCart"] = dataTable;
    }
    #endregion

    #region Method to check which row in datarow has a food which add
    public int IsExistFoodInOrderCart(string foodID, DataTable dataTable)
    {
        int indexOfRow = -1;
        for (int dataRow = 0; dataRow < dataTable.Rows.Count; dataRow++)
        {
            if (dataTable.Rows[dataRow]["FoodID"].ToString() == foodID)
            {
                indexOfRow = dataRow;
                break;
            }
        }
        return indexOfRow;
    }
    #endregion
}
